<?php

namespace Common\Lib\Upload;

use Common\Lib\Upload\ImgManager\IImgManager;

class ImgManager
{
    /**
     * @var IImgManager
     */
    private $imgManager;

    public function __construct(array $img)
    {
        $class = 'Common\\Lib\\Upload\\ImgManager\\' . ucwords($img['savetype']);
        // 根据savetype初始化imgManager值 [工厂方法]
        if ($img['savetype'] === 'local') {
            $options = array();
        } elseif ($img['savetype'] === 'qiniu') {
            $options = C('UPLOAD_TYPE_CONFIG');
        } else {
            E('未知的上传图片保存类型');
        }
        $imgManager = new $class($options);

        $this->imgManager = $imgManager;
        $this->imgManager->set_img($img);
    }

    /**
     * 静态方法实例化类
     * @param int|array $img
     * @return ImgManager
     */
    public static function make($img)
    {
        if (is_numeric($img) && is_int($img + 0)) {
            $img = D('UploadImg')->get_one_id($img);
        }
        return new self($img);
    }

    public function url()
    {
        return $this->imgManager->url();
    }

    public function path()
    {
        return $this->imgManager->path();
    }
}